<template>
  <div class="flexContent">
    <div class="searchBox">
      <!-- 商品列表 -->
      <el-form class="demo-form-inline" label-position="left" label-width="auto">
        <div class="serFlexBox">
          <div class="inputBox">
            <el-row :gutter="12" style="display: flex;flex-wrap: wrap;">
              <el-col :xs="12" :sm="12" :md="12" :lg="8" :xl="6">
                <el-form-item label="商品">
                  <el-input v-clearEmoji v-model.trim="sForm.name" size="small" clearable style="width: 95%"
                    placeholder="请输入名称" @keyup.enter.native="searchFn" />
                </el-form-item>
              </el-col>
              <el-col :xs="12" :sm="12" :md="12" :lg="8" :xl="6">
                <el-form-item label="是否上架">
                  <el-select v-model="sForm.publishStatus" style="width: 95%" clearable placeholder="请选择">
                    <el-option value="" label="全部" />
                    <el-option value="0" label="下架" />
                    <el-option value="1" label="上架" />
                  </el-select>
                </el-form-item>
              </el-col>

              <el-col :xs="12" :sm="12" :md="12" :lg="8" :xl="6">
                <el-form-item label="商品分类">
                  <el-select @click.native="getCategoryList" v-model="sForm.categoryId" style="width: 95%" filterable popper-class="eloption">
                    <el-option v-for="(val, key) in categoryList" :key="key" :value="key" :label="val" />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :xs="12" :sm="12" :md="12" :lg="10" :xl="6">
                <el-form-item label="更新时间">
                  <el-date-picker v-model="date" clearable type="daterange" unlink-panels range-separator="-"
                    start-placeholder="开始日期" end-placeholder="结束日期" value-format="yyyy-MM-dd" style="width: 95%" />
                </el-form-item>
              </el-col>
            </el-row>
          </div>
          <div class="btnBox">
            <el-button class="searchBtn" icon="el-icon-search" @click="searchFn">搜索</el-button>
            <el-button class="resetBtn" icon="el-icon-refresh-right" @click="resetFn">重置</el-button>
          </div>
        </div>
      </el-form>
    </div>

    <div class="add">
      <el-button class="addBtn" v-has="'genericShopGoods_add'" @click="addFn">新增</el-button>
    </div>

    <div class="table" id="tableBox" v-height>
      <div style="flex:1;">
        <vxe-table :column-config="{ resizable: true }" border class="tablebox" ref="xTable1"
          :row-config="{ isHover: true }" :scroll-y="{ enabled: true, gt: 21 }" style="width: 100%" height="100%"
          size="medium" align="center">
          <vxe-column type="seq" width="80" title="序号" />
          <vxe-column field="name" title="商品名称" :min-width="elTableAdaptationWidth('name', shopGoodsList)"/>
          <vxe-column field="categoryName" title="商品分类" :min-width="elTableAdaptationWidth('categoryName', shopGoodsList)"/>
          <vxe-column field="deliveryNorms" title="配送规格" min-width="150"/>
          <vxe-column field="salePrice" title="单价价格（元）" min-width="150"/>
          <vxe-column field="normsPrice" title="配送规格价格（元）" min-width="150"/>
          <vxe-column field="publishStatus" title="是否上架" width="100">
            <template v-slot="{ row }">
              <el-tag v-if="row.publishStatus === '1'" disabled type="primary" plain size="small">已上架</el-tag>
              <el-tag v-if="row.publishStatus === '0'" disabled type="info" plain size="small">已下架</el-tag>
            </template>
          </vxe-column>
          <vxe-column field="updateTime" title="更新时间" min-width="150">
            <template v-slot="{ row }">
              <span>{{ row.updateTime | parseTime }}</span>
            </template>
          </vxe-column>
          <vxe-column v-if="$showFn(['genericShopGoods_detail', 'genericShopGoods_edit', 'genericShopGoods_del'])"
            fixed="right" width="200" title="操作">
            <template v-slot="{ row }">
              <el-button type="text" v-has="'genericShopGoods_edit'" style="margin-right: 10px; color: rgb(0, 132, 255)"
                @click.stop="editGoods(row)">编辑</el-button>
              <el-button type="text" v-has="'genericShopGoods_detail'" style="margin-right: 10px; color: rgb(0, 132, 255)"
                @click.stop="goodsDetail(row)">详情</el-button>
              <el-button style="color: red;" type="text" v-if="row.publishStatus === '0'" v-has="'genericShopGoods_del'"
                @click="$delFn('商品', del, row)">删除</el-button>
            </template>
          </vxe-column>
        </vxe-table>
      </div>
      <div class="paginationBox">
        <el-pagination :current-page="page.pageNum" background :page-sizes="$pageSizes" :page-size="page.pageSize"
          layout="total,-> , sizes, prev, pager, next, jumper" :total="page.total" @size-change="handleSizeChange"
          @current-change="handleCurrentChange" />
      </div>
    </div>

  </div>
</template>

<script>
import { getDictValueAndNameSource } from '@/api/dictionary';
import { getShopNonCommodityList, deleteShopNonCommodity } from "@/api/shopManagement";
export default {
  name: "GoodsInfo",
  data() {
    return {
      drawer: false,
      title: '新增广告',
      sForm: {
        name: '',
        minDate: '',
        maxDate: '',
        publishStatus: '',
        categoryId: ''
      },
      categoryList: [],
      defaultProps: {
        children: "children",
        label: "name",
        emitPath: false,
        value: "id",
        checkStrictly: true,
      },
      date: [],
      agreement: {},
      agreementId: '',
      isSearch: false,
      page: {
        pageNum: 1,
        pageSize: 20,
        total: 0,
      },
      shopGoodsList: [],

    };
  },
  created() {
    this.getShopNonCommodityList();
    this.getCategoryList()
  },
  methods: {
    // 获取分类
    async getCategoryList() {
      const res = await getDictValueAndNameSource('NONSTANDARD_CATEGORY');
      this.categoryList = res.data;
      console.log(this.categoryList, 'this.categoryList');
    },
    async getShopNonCommodityList() {
      if (!this.isSearch) {
        this.sForm = {
          name: '',
          minDate: '',
          maxDate: '',
          publishStatus: '',
          categoryId: ''
        }
      }
      const res = await getShopNonCommodityList({
        ...this.page,
        ...this.sForm
      });
      if (res.code === 200) {
        console.log(res);
        const result = res.data;
        this.shopGoodsList = result.list;
        this.page.pageSize = result.pageSize;
        this.page.pageNum = result.pageNum;
        this.page.total = result.total;
        console.log("shopGoodsList", this.shopGoodsList);
        const $table = this.$refs.xTable1
        $table.loadData(this.shopGoodsList);
      } else {
        // this.$message.error(res.message);
      }
    },
    // 分类选择后
    handleChange(value) {
      console.log("分类", value);
      // const nodes = this.$refs.cascaderRef.getCheckedNodes();
      // console.log("nodes", nodes);
      // this.sForm.categoryId = value;
      const nodes = this.$refs.cascaderRef.getCheckedNodes();
      this.sForm.level = nodes[0]?.level;
    },
    // 获取条数改变
    handleSizeChange(val) {
      this.page.pageSize = val;
      this.page.pageNum = 1;
      console.log(`每页 ${val} 条`);
      this.getShopNonCommodityList();
    },
    // 获取页数改变
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.page.pageNum = val;
      this.getShopNonCommodityList();
    },
    checkagreement(row) {
      this.drawer = true
      this.agreement = row
    },
    async searchFn() {
      this.page = {
        pageNum: 1,
        pageSize: 20,
        total: 0,
      };
      this.sForm.minDate = this.date?.length ? this.date[0] : "";
      this.sForm.maxDate = this.date?.length ? this.date[1] : "";
      this.isSearch = true;
      this.getShopNonCommodityList();
    },
    resetFn() {
      this.page = {
        pageNum: 1,
        pageSize: 20,
        total: 0,
      };
      this.date = []
      this.isSearch = false;
      // this.selectedOptions = [];
      this.getShopNonCommodityList();
    },
    addFn() {
      this.$router.push({
        path: "genericShopGoodAdd",
        query: {
          type: "add",
        },
      })

    },
    editGoods(row) {
      this.$router.push({
        path: "genericShopGoodEdit",
        query: {
          type: "edit",
          id: row.id,
        },
      });
    },
    goodsDetail(row) {
      this.$router.push({
        path: "genericShopGoodDetail",
        query: {
          type: "detail",
          id: row.id,
        },
      });
    },
    async del(row) {
      const res = await deleteShopNonCommodity({ id: row.id })
      if (res.code === 200) {
        this.$message.success('删除成功')
        this.getShopNonCommodityList()
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.txtBox {
  padding-left: 20px;
  margin-bottom: 30px;

}

.btnBOX {
  text-align: center;
}
</style>
